package com.gitee.dupe.authority.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.gitee.dolt.annotation.security.PreAuth;
import com.gitee.dolt.boot.controller.*;
import com.gitee.dolt.boot.request.QueryParams;
import com.gitee.dolt.core.base.Result;
import com.gitee.dupe.authority.dto.*;
import com.gitee.dupe.authority.entity.User;
import com.gitee.dupe.authority.enumeration.UserStateEnum;
import com.gitee.dupe.authority.service.IUserService;
import com.gitee.dupe.common.dto.UserGeneralDTO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;


/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author LYH
 * @since 2020-06-30
 */
@RestController
@RequestMapping("/b/user")
@Validated
@Api(value = "User", tags = "用户")
@RequiredArgsConstructor
@PreAuth(replace = "b:user:")
public class UserController extends SuperCacheController<User, IUserService, Long, UserSaveDTO, UserUpdateDTO, UserQueryDTO> {

    @Override
    public QueryWrapper<User> handlerQueryWrapper(User user, QueryParams<UserQueryDTO> params) {
        String keyword = params.getKeyword();
        QueryWrapper<User> qw = Wrappers.query();
        if (StrUtil.isNotEmpty(keyword)) {
            qw.lambda().and(e -> e.eq(User::getAccount, keyword)
                    .or().eq(User::getRealName, keyword)
                    .or().eq(User::getMobile, keyword)
                    .or().eq(User::getEmail, keyword));
        }
        UserQueryDTO model = params.getModel();
        UserStateEnum state = model.getState();
        String group = model.getGroup();
        if (state != null) {
            qw.lambda().eq(User::getState, state);
        }
        if (StrUtil.isNotEmpty(group)) {
            qw.lambda().eq(User::getGroup, group);
        }
        return qw;
    }

    @Override
    public Result handlerGet(Long id) {
        return Result.ok(baseService.getDetail(id));
    }

    @Override
    public Result handlerSave(UserSaveDTO userSaveDTO) {
        return Result.ok(baseService.handlerSave(userSaveDTO));
    }

    @Override
    public Result handlerUpdate(UserUpdateDTO userUpdateDTO) {
        return Result.ok(baseService.handlerUpdate(userUpdateDTO));
    }

    @PutMapping("/{id}/roles")
    @ApiOperation(value = "用户授予角色")
    @PreAuth("hasPermission('{}grantRoles')")
    public Result grantRoles(@PathVariable Long id, @RequestBody Set<Long> roleIds) {
        baseService.grantRoles(id, roleIds);
        return Result.ok();
    }

//    @PutMapping("/{id}/changeState")
//    @ApiOperation(value = "修改用户状态")
//    @PreAuth("hasPermission('{}changeState')")
//    public Result changeState(@PathVariable Long id, @RequestBody UserChangeStateDTO userChangeStateDTO) {
//        baseService.changeState(id, userChangeStateDTO.getState());
//        return Result.ok();
//    }

    @PutMapping("/{id}/resetPassword")
    @ApiOperation(value = "用户重置密码")
    @PreAuth("hasPermission('{}resetPassword')")
    public Result resetPassword(@PathVariable Long id, @RequestBody UserResetPasswordDTO userResetPasswordDTO) {
        baseService.resetPassword(id, userResetPasswordDTO.getPassword());
        return Result.ok();
    }

    @PostMapping("/listByResourceIdents")
    @ApiOperation(value = "根据资源标识获取用户")
    public Result listByResourceIdents(@RequestBody List<String> idents) {
        List<UserGeneralDTO> users = baseService.findAllByResourceIdents(idents);
        return Result.ok(users);
    }


}
